﻿using System;
using System.Windows.Media;
using Tanis.RouteService;
using System.Windows.Data;
using System.Windows;
using System.Text;
using Microsoft.Phone.Controls;

namespace Tanis.Controls
{
    public class DesignHelper
    {
        public static string FormatTime(DateTime dateTime)
        {
            return string.Format("{0}:{1}", dateTime.Hour, dateTime.Minute < 10 ? "0" + dateTime.Minute : dateTime.Minute.ToString());
        }

        public static string GetColor(BusRideSegment segment)
        {
            string transport = segment.Transport;

            if (transport.EndsWith("TrolleyBus"))
                return "#FF3737"; //"#E6097F";

            if (transport.EndsWith("RajDBBus"))
                return "#3043E7"; //"#4D8BFF";

            if (transport.EndsWith("CommBus"))
                return "#B600B6"; 

            if (transport.EndsWith("Bus"))
                return "#60B0E0"; //"#0A91FF";

            return "#B600B6"; // Colors.White.ToString();
        }

        public static string GetIcon(BusRideSegment segment)
        {
            string transport = segment.Transport;
            //return "/Tanis;component/Images/appbar.delete.rest.png";

            if (transport.EndsWith("TrolleyBus"))
                return "/Tanis;component/Images/troll2.png";

            if (transport.EndsWith("RajDBBus"))
                return "/Tanis;component/Images/other_bus2.png";

            if (transport.EndsWith("CommBus"))
                return "/Tanis;component/Images/private_bus2.png";

            if (transport.EndsWith("Bus"))
                return "/Tanis;component/Images/bus2.png";

            return "/Tanis;component/Images/private_bus2.png";
        }

        public static string GetTransportDescription(BusRideSegment segment)
        {
            string transport = segment.Transport;

            if (transport.EndsWith("TrolleyBus"))
                return "Trolleybus";

            if (transport.EndsWith("DBBus"))
                return "Regional Bus";

            if (transport.EndsWith("Bus"))
                return "Bus";

            return "Private bus";
        }
    }

    public class VisibilityConverter : IValueConverter
    {
        public Visibility True { get; set; }
        public Visibility False { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value != null ? True : False);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //throw new NotImplementedException();
            return null;
        }
    }

    public static class StringExtensions
    {
        private static char[] lithuanianReplace = { 'a', 'c', 'e', 'e', 'i', 's', 'u', 'u', 'A', 'C', 'E', 'E', 'I', 'S', 'U', 'U' };
        private static char[] lithuanianAccents = { 'ą', 'č', 'ę', 'ė', 'į', 'š', 'ų', 'ū', 'Ą', 'Č', 'Ę', 'Ė', 'Į', 'Š', 'Ų', 'Ū' };

        public static string RemoveDiacritics(this string accentedStr)
        {
            StringBuilder sbStripAccents = new StringBuilder();
            if (accentedStr.IndexOfAny(lithuanianAccents) > -1)
            {
                sbStripAccents.Length = 0;
                sbStripAccents.Append(accentedStr);
                for (int i = 0; i < lithuanianAccents.Length; i++)
                {
                    sbStripAccents.Replace(lithuanianAccents[i], lithuanianReplace[i]);
                }
                return sbStripAccents.ToString();
            }
            else
            {
                return accentedStr;
            }
        }
    }

    public class AutoCompleteVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool visible = true;

            if (value is bool)
            {
                visible = (bool)value;
            }
            else if (value is int || value is short || value is long)
            {
                visible = 0 != (int)value;
            }
            else if (value is float || value is double)
            {
                visible = 0.0 != (double)value;
            }
            else if (value == null)
            {
                visible = false;
            }

            if ((string)parameter == "!")
            {
                visible = !visible;
            }

            return visible ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class AutoCompleteworkerParameter
    {
        public AutoCompleteBox AutoCompleteBox { get; set; }
        public string CurrentText { get; set; }
        public object Result { get; set; }
    }

    public static class ThemeHelper
    {
        private static Color? _themeDependantColor = null;
        public static Color ThemeDependantColor
        {
            get
            {
                if (_themeDependantColor == null)
                {
                    _themeDependantColor = ((SolidColorBrush)Application.Current.Resources["PhoneBackgroundBrush"]).Color;
                }
                return _themeDependantColor.Value;
            }
        }

        private static Color? _themeDependantColorInverted = null;
        public static Color ThemeDependantColorInverted
        {
            get
            {
                if (_themeDependantColorInverted == null)
                {
                    Color backgroundColor = ((SolidColorBrush)Application.Current.Resources["PhoneBackgroundBrush"]).Color;
                    if (backgroundColor == Colors.White)
                    {
                        _themeDependantColorInverted = Colors.Black;
                    }
                    else
                    {
                        _themeDependantColorInverted = Colors.White;
                    }
                }
                return _themeDependantColorInverted.Value;
            }
        }

        private static Color? _themeDependantListBoxColor = null;
        public static Color ThemeDependantListBoxColor
        {
            get
            {
                if (_themeDependantListBoxColor == null)
                {
                   
                    Color backgroundColor = ((SolidColorBrush)Application.Current.Resources["PhoneBackgroundBrush"]).Color;
                    if (backgroundColor == Colors.White)
                    {
                        _themeDependantListBoxColor = new Color() { A = 255, R = 240, G = 240, B = 240 };
                            //Colors.White;
                    }
                    else
                    {
                        _themeDependantListBoxColor = new Color() { A = 255, R = 51, G = 51, B = 51 };
                    }
                }
                return _themeDependantListBoxColor.Value;
            }
        }
    }
}
